
package cn.yunhe.controller;

import cn.yunhe.entity.Field;
import cn.yunhe.entity.KnowledgePoint;
import cn.yunhe.service.impl.KnowledgePointServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class KnowledgePointController {
    @Resource
    private KnowledgePointServiceImpl knowledgePointService;

    @RequestMapping("/knowledgeByPage")
    public Map queryKnowledgeByPage(int pageSize, int pageNum, KnowledgePoint knowledgePoint) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("knowledgePoint", knowledgePoint);
        Page page = knowledgePointService.getKnowledgePointListByPage(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());

        return map;
    }

    @RequestMapping("/addKnowledgePoint")
    public int addKnowledgePoint(KnowledgePoint knowledgePoint ,int fid) {
        Field field = new Field();
        field.setFieldId(fid);
        knowledgePoint.setField(field);
        return knowledgePointService.addKnowledgePoint(knowledgePoint);
    }

    @RequestMapping("/delKnowledgePoint")
    public int delKnowledgePoint(KnowledgePoint knowledgePoint) {
        return knowledgePointService.delKnowledgePoint(knowledgePoint);
    }

    @RequestMapping("getKnowledgePointById")
    public ModelAndView getKnowledgePointById(KnowledgePoint knowledgePoint, HttpServletRequest request) {
        KnowledgePoint k = knowledgePointService.getKnowledgePointById(knowledgePoint);
        request.setAttribute("data", k);
        return new ModelAndView("updKnowledgePoint");
    }

    @RequestMapping("/updKnowledgePoint")
    public int updKnowledgePoint(KnowledgePoint knowledgePoint ,int fid) {

        Field field = new Field();
        field.setFieldId(fid);
        knowledgePoint.setField(field);
        return knowledgePointService.updKnowledgePoint(knowledgePoint);
    }

    public KnowledgePointServiceImpl getKnowledgePointService() {
        return knowledgePointService;
    }

    public void setKnowledgePointService(KnowledgePointServiceImpl knowledgePointService) {
        this.knowledgePointService = knowledgePointService;
    }
}

